package com.baosight.xdata.monitor;

import com.baosight.xdata.monitor.utils.JedisUtil;
import com.baosight.xdata.monitor.utils.RedisUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Slowlog;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.baosight.xdata.monitor.entity.RedisNodeInfoUtil.*;

public class TssRedisMonitorByJedisTest {

    private static final Logger logger = LoggerFactory.getLogger(TssRedisMonitorByJedisTest.class);

//        private static final String ipAddr = "10.25.10.105";
//        private static final int port = 6378;

        private static final String ipAddr = "10.25.10.104";
        private static final int port = 6377;

        private static Jedis jedis = null;

    @Before
    public void init() {
        jedis = JedisUtil.getInstance().getJedis(ipAddr, port);
    }

    @After
    public void close() {
        JedisUtil.getInstance().closeJedis(jedis, ipAddr, port);
    }
    //=========================================================================================================================================================================================

    /**
     * clients
     */
    @Test
    public void clients() {
        Map<String, String> clients;
        try {
            clients = RedisUtil.parseInfoToMap(jedis.info("clients"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(clients);
    }

    /**
     * cluster
     */
    @Test
    public void cluster() {
        Map<String, String> cluster;
        try {
            cluster = RedisUtil.parseInfoToMap(jedis.info("cluster"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(cluster);
    }

    /**
     * commandstats
     */
    @Test
    public void commandstats() {
        Map<String, String> commandstats;
        try {
            commandstats = RedisUtil.parseInfoToMap(jedis.info("commandstats"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(commandstats);
    }

    /**
     * cpu
     */
    @Test
    public void cpu() {
        Map<String, String> cpu;
        try {
            cpu = RedisUtil.parseInfoToMap(jedis.info("cpu"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(cpu);
    }

    /**
     * errorstats
     */
    @Test
    public void errorStats() {
        Map<String, String> errorstats;
        try {
            errorstats = RedisUtil.parseInfoToMap(jedis.info("errorstats"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(errorstats);
    }

    /**
     * keyspace
     */
    @Test
    public void keyspace() {
        Map<String, String> keyspace;
        try {
            keyspace = RedisUtil.parseInfoToMap(jedis.info("keyspace"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(keyspace);
    }

    /**
     * latencystats
     */
    @Test
    public void latencystats() {
        Map<String, String> latencystats;
        try {
            latencystats = RedisUtil.parseInfoToMap(jedis.info("latencystats"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(latencystats);
    }

    /**
     * memory
     */
    @Test
    public void memory() {
        Map<String, String> memory;
        try {
            memory = RedisUtil.parseInfoToMap(jedis.info("memory"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(memory);
    }

    /**
     * persistence
     */
    @Test
    public void persistence() {
        Map<String, String> persistence;
        try {
            persistence = RedisUtil.parseInfoToMap(jedis.info("persistence"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(persistence);
    }

    /**
     * replication
     */
    @Test
    public void replication() {
        Map<String, String> replication;
        try {
            replication = RedisUtil.parseInfoToMap(jedis.info("replication"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(replication);
    }

    /**
     * server
     */
    @Test
    public void server() {
        Map<String, String> server;
        try {
            server = RedisUtil.parseInfoToMap(jedis.info("server"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(server);
    }

    /**
     * stats
     */
    @Test
    public void stats() {
        Map<String, String> stats;
        try {
            stats = RedisUtil.parseInfoToMap(jedis.info("stats"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(stats);
    }


    //=========================================================================================================================================================================================

    /**
     * 获取maxmemory 0表示全部linux系统内存
     * used_memory 1表示使用全部内存
     * maxmemory_policy
     */
    @Test
    public void getMemoryInfo() {
        try {
            Map<String, String> memoryInfo = RedisUtil.parseInfoToMap(jedis.info("memory"));
            System.out.println(memoryInfo);
            if (memoryInfo.isEmpty()) {
                return;
            }
            System.out.println(byteToMB(memoryInfo.get("maxmemory")));
            System.out.println(byteToMB(memoryInfo.get("used_memory")));
            System.out.println(memoryInfo.get("maxmemory_policy"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取redis集群信息
     * 集群状态
     */
    @Test
    public void getClusterInfo() {
        try {
            Map<String, String> clusterInfo = RedisUtil.parseInfoToMap(jedis.clusterInfo());
            System.out.println(clusterInfo);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取redis版本
     * redis模式 standalone、cluster
     * 最大客户端连接数
     * 已连接客户端
     * 拒绝连接数
     * 集群主从信息
     * 错误回复总数
     * 执行的总命令条数
     * 每秒执行的总命令条数
     * 获取maxmemory 0表示全部linux系统内存
     * used_memory 1表示使用全部内存
     * maxmemory_policy
     *
     */
    @Test
    public void getNodeInfo() {
        try {
            Map<String, String> nodeInfo = RedisUtil.parseInfoToMap(jedis.info());
            System.out.println("redis版本: "+ nodeInfo.get("redis_version"));
            System.out.println("redis模式: "+ nodeInfo.get("redis_mode"));
            System.out.println("最大客户端连接数: "+ nodeInfo.get("maxclients"));
            System.out.println("已连接客户端: "+ nodeInfo.get("connected_clients"));
            System.out.println("拒绝连接数总数: "+ nodeInfo.get("rejected_connections"));
            System.out.println("错误回复总数: "+ nodeInfo.get("total_error_replies"));
            System.out.println("执行的总命令条数: "+ nodeInfo.get("total_commands_processed"));
            System.out.println("每秒执行的总命令条数: "+ nodeInfo.get("instantaneous_ops_per_sec"));
            System.out.println(byteToMB(nodeInfo.get("maxmemory")));
            System.out.println(byteToMB(nodeInfo.get("used_memory")));
            System.out.println(nodeInfo.get("maxmemory_policy"));

            // 集群主从信息
            String clusterNodes = jedis.clusterNodes();
            System.out.println(clusterNodes);

            // 连接角色
            Map<String, String> infoMap = RedisUtil.parseInfoToMap(jedis.info("replication"));
            System.out.println(infoMap);

            List<Object> objects = jedis.clusterSlots();
            System.out.println(objects);

            jedis.info("");


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * KEY
     */
    @Test
    public void getKeyspace() {
        try {
            Map<String, String> keyspace = RedisUtil.parseInfoToMap(jedis.info("keyspace"));
            System.out.println(keyspace);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取redis网络输入输出
     */
    @Test
    public void getNetWorkInfo() {
        try {
            Map<String, String> networkInfo = RedisUtil.parseInfoToMap(jedis.info());
            System.out.println(byteToMB(networkInfo.get("total_net_input_bytes")));
            System.out.println(byteToMB(networkInfo.get("total_net_output_bytes")));

            System.out.println(byteToMB(networkInfo.get("total_net_repl_input_bytes")));
            System.out.println(byteToMB(networkInfo.get("total_net_repl_output_bytes")));

            System.out.println(networkInfo.get("client_recent_max_input_buffer"));
            System.out.println(networkInfo.get("client_recent_max_output_buffer"));

            System.out.println("每秒网络输入：" + networkInfo.get("instantaneous_input_kbps") + "Kb/s");
            System.out.println("每秒网络输出：" + networkInfo.get("instantaneous_output_kbps") + "Kb/s");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * slow log
     */
    @Test
    public void slowlogGet() {
        try {
            List<Slowlog> slowlogs = jedis.slowlogGet();
            System.out.println(slowlogs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }






}
